<?php
namespace Inter\ArticleBundle\Doctrine;

use Doctrine\Common\Persistence\ObjectManager;
use Inter\ArticleBundle\Model\ArticleInterface;
use Inter\ArticleBundle\Model\ArticleManager as BaseArticleManager;
use Inter\ArticleBundle\Util\SlugifyInterface;

class ArticleManager extends BaseArticleManager
{
	protected $objectManager;
	protected $class;
	protected $repository;
	
	public function __construct(SlugifyInterface $slugSlugify, ObjectManager $om, $class)
	{
		parent::__construct($slugSlugify);
		$this->objectManager = $om;
		$this->repository = $om->getRepository($class);
		
		$metadata = $om->getClassMetadata($class);
		$this->class = $metadata->getName();
	}
	/**
	 * (non-PHPdoc)
	 * @see \Inter\ArticleBundle\Model\ArticleManagerInterface::deleteArticle()
	 */
	public function deleteArticle(ArticleInterface $article)
	{
		$this->objectManager->remove($article);
		$this->objectManager->flush();
	}
	/**
	 * (non-PHPdoc)
	 * @see \Inter\ArticleBundle\Model\ArticleManagerInterface::getClass()
	 */
	public function getClass()
	{
		return $this->class;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \Inter\ArticleBundle\Model\ArticleManagerInterface::findArticles()
	 */
	public function findArticles()
	{
		$this->repository->findAll();
	}
	/**
	 * (non-PHPdoc)
	 * @see \Inter\ArticleBundle\Model\ArticleManagerInterface::findArticleBy()
	 */
	public function findArticleBy(array $criteria)
	{
		return $this->repository->findOneBy($criteria);
	}
	/**
	 * (non-PHPdoc)
	 * @see \Inter\ArticleBundle\Model\ArticleManagerInterface::updateArticle()
	 */
	public function updateArticle(ArticleInterface $article, $andFlush = true)
	{
		$this->updateSlugField($article);
		$this->objectManager->persist($article);
		if ($andFlush){
			$this->objectManager->flush();
		}
	}
}

